﻿
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

namespace Simulation.Framework
{
    public class SceneManager : ManagerBehavior
    {
        public SceneBase activeScene { get; private set; }

        private IEnumerator loadSceneIE;

        private string lastSceneName = string.Empty;

        #region override

        public override void Init() { }

        public override void Restar() { }

        public override void Clear()
        {
            loadSceneIE = null;
            activeScene = null;
        }

        #endregion

        public void ChangeScene<T>(string sceneName, LoadSceneMode model = LoadSceneMode.Single, UnityAction<T> onComplete = null) where T : SceneBase
        {
            if (activeScene == null)
                activeScene = gameObject.GetComponent<T>();//GameObject.FindObjectOfType<T>(); //
            if (activeScene != null)
            {
                activeScene.OnRemove();
                Destroy(activeScene);
            }
            lastSceneName = sceneName;
            if (loadSceneIE != null)
                StopCoroutine(loadSceneIE);
            loadSceneIE = LoadSceneIE<T>(sceneName, model, onComplete);
            StartCoroutine(loadSceneIE);
        }
        private IEnumerator LoadSceneIE<T>(string sceneName, LoadSceneMode model, UnityAction<T> onComplete) where T : SceneBase
        {
            AsyncOperation asyncOperation = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, model);

            yield return asyncOperation;
 
            Log.Info("SceneManager>> load complete ::" + sceneName);
            T scene =  gameObject.GetOrAddComponent<T>();//GameObject.FindObjectOfType<T>();//
            scene.OnLoaded();
            activeScene = scene;
            Scene sceneActionName = UnityEngine.SceneManagement.SceneManager.GetSceneByName(sceneName);
            Debug.Log(sceneActionName.buildIndex);
            bool isScene= UnityEngine.SceneManagement.SceneManager.SetActiveScene(sceneActionName);
            onComplete?.Invoke(scene);
        }
        //---------------------==============================-----------------------------------
        public void UnloadScene(string sceneName, UnityAction onComplete = null)
        {
 
            Scene sceneActionName= UnityEngine.SceneManagement.SceneManager.GetSceneByName(sceneName);
            Debug.Log(sceneActionName.buildIndex);
           // bool isScene= UnityEngine.SceneManagement.SceneManager.SetActiveScene(sceneActionName);
            if (sceneActionName.buildIndex==1)
            {
                if (loadSceneIE != null)
                    StopCoroutine(loadSceneIE);
                loadSceneIE = UnloadSceneAsyncIE(sceneName, onComplete);
                StartCoroutine(loadSceneIE);
            }
            else
            {
                onComplete?.Invoke();
            }
        }
 
        public void UnloadScene(UnityAction onComplete = null) 
        {

            if (!string.IsNullOrEmpty(lastSceneName))
            {
                if (loadSceneIE != null)
                    StopCoroutine(loadSceneIE);
                loadSceneIE = UnloadSceneAsyncIE(lastSceneName, onComplete);
                StartCoroutine(loadSceneIE);
            }
            else
            {
                onComplete?.Invoke();
            }

        }
        private IEnumerator UnloadSceneAsyncIE(string sceneName, UnityAction onComplete) 
        {
            AsyncOperation asyncOperation = UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync(sceneName);
            lastSceneName = string.Empty;
            yield return asyncOperation;
            onComplete?.Invoke();
           
        }
    }
}